package com.carson.undergo.ui.dialog.cart;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.carson.undergo.R;
import com.carson.undergo.ui.activity.home.CartDetailUI;
import com.carson.undergo.ui.adapter.cart.PopupCartAdapter;
import com.carson.undergo.utils.bean.CartGoodsBean;

/**
 * Create by Carson on 2021/12/24.
 * 购物车弹框
 */
public class ShopCartDialog extends Dialog implements ShopCartImp, View.OnClickListener {

    private final CartDetailUI cartActivity;
    private LinearLayout layoutCart;

    public ShopCartDialog(Activity activity, Context context, int themeResId) {
        super(context, themeResId);
        this.cartActivity = (CartDetailUI) activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_cart_info);
        layoutCart = findViewById(R.id.layout_cart);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_cart);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        PopupCartAdapter dishAdapter = new PopupCartAdapter(cartActivity, cartActivity, cartActivity.cartList);
        recyclerView.setAdapter(dishAdapter);
        dishAdapter.setShopCartImp(this);
        cartActivity.setCartNumber();

        findViewById(R.id.clear_cart).setOnClickListener(this);
        findViewById(R.id.view_cancel).setOnClickListener(this);
        findViewById(R.id.view_bottom).setOnClickListener(this);
    }

    @Override
    public void show() {
        super.show();
        animationShow();
    }

    @Override
    public void dismiss() {
        animationHide();
    }


    @SuppressLint("Recycle")
    private void animationShow() {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(layoutCart, "translationY", 1000, 0).setDuration(500)
        );
        animatorSet.start();
    }

    @SuppressLint("Recycle")
    private void animationHide() {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(layoutCart, "translationY", 0, 1000).setDuration(500)
        );
        animatorSet.start();

        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ShopCartDialog.super.dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    @Override
    public void add(View view, int position) {
        cartActivity.setCartNumber();

    }

    @Override
    public void remove(View view, int position) {
        cartActivity.setCartNumber();
        if (cartActivity.mCount == 0) {
            this.dismiss();
        }
    }

    private long lastTime = 0L;
    private View currentView = null;

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastTime > 1000L || view.getId() == currentView.getId()) {
            lastTime = nowTime;
            currentView = view;
            switch (view.getId()) {
                case R.id.view_cancel:
                case R.id.view_bottom:
                    dismiss();
                    break;
                case R.id.clear_cart:
                    clear();
                    break;
            }
        }
    }

    public void clear() {
        for (CartGoodsBean item : cartActivity.cartList) {
            cartActivity.notifyMainGoods(item.getSkuId(), 0);
        }
        cartActivity.mCount = 0;
        cartActivity.cartList.clear();
        cartActivity.setCartNumber();
        this.dismiss();
    }

}
